<?php
header('content-type:text/html;charset=utf-8');
$host = "localhost";
$user = "root";
$password = "";
$db = "library";


$mysqli = new mysqli($host, $user, $password, $db); //实例化mysqli对象，连接mysql数据库

//获取输入的用户名 
$number = $_POST['number'];
$bookname = $_POST['bookname'];
$aythor = $_POST['aythor'];
//向数据库查询是否存在相同的用户名
$query = "SELECT * FROM book WHERE number = '$number'"; 
$result = mysqli_query($mysqli, $query); //如果结果集中有数据，则用户名已存在 

if(mysqli_num_rows($result) > 0) {
    $pd = "SELECT * FROM book WHERE number = '$number'"; 
    $res = mysqli_query($mysqli, $pd);
    
    // echo "<script>alert('密码错误！');window.location.href='id.html'</script>"; 
    if($number ==''){
        echo "<script>alert('请输入编号');window.location.href='revise.html'</script>"; 
     }
    
    else if($bookname == ''){
        echo "<script>alert('请输入新书名！');window.location.href='revise.html'</script>"; 
    }
    else if($aythor == ''){
        echo "<script>alert('请输入新作者！');window.location.href='revise.html'</script>"; 
    }
    
    else if(mysqli_num_rows($res) > 0){
        $sql = "UPDATE book SET bookname ='$bookname', aythor='$aythor' WHERE number = $number;";
        mysqli_query($mysqli,$sql);
        mysqli_close($mysqli);
        echo "<script>alert('修改成功！');window.location.href='revise.html'</script>"; 
    }
    else{
    echo "<script>alert('b输入错误！');window.location.href='revise.html'</script>"; 

    }
}
else {            
    echo "<script>alert('编号输入错误！');window.location.href='revise.html'</script>"; 
}

?>